package caesar;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CaesarEncode extends FilterOutputStream {
	private int key;
	public CaesarEncode(OutputStream in, int keys) {
		super(in);
		key = keys;
	}

	public void write(char c ) throws IOException{
		 super.write((encode(c)));
	}
	public void write(String s) throws IOException {
		char[] k = s.toCharArray();
		for(int i=0; i<s.length();i++)
			write(k[i]);
		
	}
	
	//Character.isLetter(help) &&
	private char encode(char clear){
		char help = clear;
		if((help >= 'A' && help <= 'Z') || (help >= 'a' && help <= 'z'))
			if (Character.isUpperCase(help))
				if ((help - key) < 65)
					help = (char) (help - key + 26);
				else
					help -= key;
			else
				if ((help - key) < 97)
					help = (char) (help - key + 26);
				else
					help -= key;
		return help;
	}
}

